<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\DeleteOp;
use App\Admin\Extensions\UserCancelLogOp;
use App\Http\Controllers\Controller;
use App\Model\CallComment;
use App\Model\User;
use App\Model\UserCancelLog;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

/**
 * 通话评价
 */
class CallCommentController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('通话评价管理');
        $content->body($this->grid());
        return $content;
    }

    /**
     *  $model->call_id = $callRecord->id;
        $model->call_type = $callRecord->call_type;
        $model->channel_id = $callRecord->channel_id;
        $model->level = $level;
        $model->tag = $tag;
        $model->desc = $desc;
     */
    protected function grid()
    {
        $grid = new Grid(new CallComment());
        $where = [
            // ['flag', 1],
        ];
        $grid->model()->where($where)->orderBy('id', 'desc');
        $grid->channel_id('频道ID');
        $grid->level('评价')->display(function ($data) {
            if (!$data) return '-';
            return CallComment::LEVEL[$data] ?? "-";
        });
        $grid->tag('标签')->display(function ($data) {
            if (!$data) return '-';
            $tags = CallComment::getTagsById(explode(',', $data));
            return !$data ? '-' : implode('，', $tags);
        });
        $grid->desc('评价详情');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->nick_name('用户昵称');
        $grid->created_at('评价时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->flag('记录状态')->display(function ($data) {
            return $data == 1 ? '有效' : '已删除';
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('channel_id', '频道ID');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('call_type', '话单类型')->select([0 => '语音', 1 => '视频']);
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new DeleteOp($actions->getKey(), $actions->row, 'call_comment/verify'));
        });
        return $grid;
    }

    function verify()
    {
        $id = request('id');
        $status = intval(request('status'));
        $record = CallComment::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //修改状态
        $record->update(['flag' => $status]);
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
